/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.servinf.ani.sincronizador.service;

import com.servinf.ani.mapper.AssignmentMapper;
import com.servinf.ani.sincronizador.dao.AssignmentDAO;
import com.servinf.ani.sincronizador.entities.Intervencion;
import com.servinf.ani.sincronizador.exception.IncompatibleTypesException;
import com.servinf.ani.sincronizador.utils.LogHelper;
import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author mortiz
 */
@Component
public class AssignmentService {
    
        private AssignmentDAO assginmentDAO;
 
        public AssignmentDAO getAssignmentDAO() {
            return assginmentDAO;
        }
        
        @Autowired
        public void setAssignmentDAO(AssignmentDAO assginmentDAO) {
            this.assginmentDAO = assginmentDAO;
        }
   
        public List<Intervencion> findALl(){
            List<Intervencion> assginmentList = getAssignmentDAO().findAll();
            return assginmentList;
        }
        
        
        public void create(List<Object> excelRow, Intervencion assginment) throws IncompatibleTypesException, IOException  {
            Intervencion assignmentNew = AssignmentMapper.excelToAssignment(excelRow, assginment);
            if(assignmentNew != null) {
               getAssignmentDAO().create(assignmentNew); //Se crea el tramo
               LogHelper.Log(LogHelper.INFO, "OK Intervención Creada ", null);
               update(excelRow, assignmentNew); //Se actualizan las relaciones
               LogHelper.Log(LogHelper.INFO, "Relaciones asignadas a la intervenciòn nueva creada con Id :  " + assignmentNew.getIdIntervencion(), null);
            }
        }
        
        
        public void update(List<Object> excelRow, Intervencion assginment) throws IncompatibleTypesException, IOException {
            Intervencion assignmentUpdated = AssignmentMapper.excelToAssignment(excelRow, assginment);
            if(assignmentUpdated != null) {
                getAssignmentDAO().update(assignmentUpdated);
                LogHelper.Log(LogHelper.INFO, "OK Intervención Actualizada ", null);
            }
        }
}


